package com.example.demo.controllerApi;

import cn.hutool.json.JSONArray;
import com.example.demo.service.UserInfoTestService;
import com.example.demo.util.AjaxJson;
import com.example.demo.util.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: Pinp
 * @Date: 2021/7/21 21:53
 */

@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserApi {

    @Autowired
    private UserInfoTestService us;

    @RequestMapping("getUserInfo")
    public AjaxJson getUserInfo(@RequestParam int userId) throws CustomException {
        String dataStr = us.getUserInfo(userId);
//        log.info(dataStr);
        JSONArray jsonArray = new JSONArray(dataStr);
        if (jsonArray.size() > 1){
            throw new CustomException("数据错误：获取到多个用户");
        }
        return AjaxJson.getSuccessData(jsonArray);
    }


}
